package com.kmxd.ams.controller.use;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.jy.dto.StatisticsAddDTO;
import com.kmxd.ams.client.jy.vo.StatisticsVO;
import com.kmxd.ams.client.use.IUseOrderSvc;
import com.kmxd.ams.client.use.dto.*;
import com.kmxd.ams.client.use.vo.UseOrderCodeVO;
import com.kmxd.ams.client.use.vo.UseOrderListVO;
import com.kmxd.ams.client.use.vo.UseOrderVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 跨全宗-单位利用主表 前端控制器
 *
 * @author ZuoLG
 * @since 2022-08-02
 */
@RestController
@Tag(name = "跨全宗-单位利用主表")
@RequestMapping("/use/useOrder")
@AllArgsConstructor
public class UseOrderCtl {

  private IUseOrderSvc useOrderService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.LOW,
      title = "分页查询")
  public R<Page<UseOrderListVO>> selectPage(Page reqPage, UseOrderQueryDTO req) {
    return R.success(useOrderService.selectPage(reqPage, req));
  }

  /** 列表查询 */
  @Operation(summary = "列表查询")
  @GetMapping("/selectList")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.LOW,
      title = "列表查询")
  public R<List<UseOrderListVO>> selectList(UseOrderQueryDTO req) {
    return R.success(useOrderService.selectList(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.LOW,
      title = "新增")
  public R<UseOrderVO> add(@Validated @RequestBody UseOrderAddDTO req) {
    return R.success(useOrderService.add(req));
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.HIGH,
      title = "修改")
  public R<Void> edit(@Validated @RequestBody UseOrderEditDTO req) {
    useOrderService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.HIGH,
      title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    useOrderService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.LOW,
      title = "查看")
  public R<UseOrderVO> view(@RequestParam Long id) {
    return R.success(useOrderService.view(id));
  }
  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.HIGH,
      title = "导出")
  @SneakyThrows
  public void export(UseOrderQueryDTO req, HttpServletResponse response) {
    String filename = "跨全宗-单位利用主表列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(UseOrderListVO.class)
        .sheet("跨全宗-单位利用主表列表")
        .doWrite(useOrderService.selectList(req));
  }

  /** 管理 */
  @Operation(summary = "管理")
  @PostMapping("/manage")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.HIGH,
      title = "管理")
  public R<Void> manage(@Validated @RequestBody UseOrderManageDTO req) {
    useOrderService.operate(req);
    return R.success();
  }

  /** 审核 */
  @Operation(summary = "审核")
  @PostMapping("/audit")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.HIGH,
      title = "审核")
  public R<Void> audit(@Validated @RequestBody UseOrderAuditDTO req) {
    useOrderService.audit(req);
    return R.success();
  }

  /** 录入评价 */
  @Operation(summary = "录入评价")
  @PostMapping("/evalEdit")
  @AutoLog(
          module = "跨全宗-单位利用主表",
          type = LogTypeEnum.ARCHIVE_USE,
          level = LogLevelEnum.HIGH,
          title = "录入评价")
  public R<Void> evalEdit(@Validated @RequestBody StatisticsAddDTO req) {
    useOrderService.evalEdit(req);
    return R.success();
  }

  /** 查看评价 */
  @Operation(summary = "查看评价")
  @GetMapping("/viewEvaluate")
  @AutoLog(
          module = "跨全宗-单位利用主表",
          type = LogTypeEnum.ARCHIVE_USE,
          level = LogLevelEnum.LOW,
          title = "查看评价")
  public R<StatisticsVO> viewEvaluate(@RequestParam Long id) {
    return R.success(useOrderService.viewEvaluate(id));
  }

  /** 管理 */
  @Operation(summary = "借阅单号")
  @GetMapping("/useCode")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.HIGH,
      title = "借阅单号")
  public R<UseOrderCodeVO> useCode() {
    return R.success(useOrderService.useCode());
  }




  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/page")
  @AutoLog(
      module = "跨全宗-单位利用主表",
      type = LogTypeEnum.ARCHIVE_USE,
      level = LogLevelEnum.LOW,
      title = "分页查询")
  public R<Page<UseOrderListVO>> list(Page reqPage, UseOrderQueryDTO req) {
    return R.success(useOrderService.selectUserPage(reqPage, req));
  }
}
